package com.example.controller;

import com.example.common.PageResult;
import com.example.common.Result;
import com.example.dto.AdminInventoryQueryDTO;
import com.example.common.RequirePermission;
import com.example.service.AdminInventoryService;
import com.example.vo.AdminInventoryRecordVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员库存管理Controller
 */
@Slf4j
@RestController
@RequestMapping("/admin/inventory")
@RequiredArgsConstructor
public class AdminInventoryController {

    private final AdminInventoryService adminInventoryService;

    /**
     * 分页查询管理员库存记录
     */
    @GetMapping("/records/page")
    @RequirePermission("admin:inventory:records:view")
    public Result<PageResult<AdminInventoryRecordVO>> getRecordsPage(AdminInventoryQueryDTO queryDTO) {
        return adminInventoryService.getRecordsPage(queryDTO);
    }

    /**
     * 根据商品ID查询库存记录
     */
    @GetMapping("/records/product/{productId}")
    @RequirePermission("admin:inventory:records:view")
    public Result<List<AdminInventoryRecordVO>> getRecordsByProductId(@PathVariable Long productId) {
        return adminInventoryService.getRecordsByProductId(productId);
    }

    /**
     * 查询入库记录（新增商品）
     */
    @GetMapping("/in-records/page")
    @RequirePermission("admin:inventory:in:view")
    public Result<PageResult<AdminInventoryRecordVO>> getInRecordsPage(AdminInventoryQueryDTO queryDTO) {
        return adminInventoryService.getInRecordsPage(queryDTO);
    }

    /**
     * 查询出库记录（分配给商家）
     */
    @GetMapping("/out-records/page")
    @RequirePermission("admin:inventory:out:view")
    public Result<PageResult<AdminInventoryRecordVO>> getOutRecordsPage(AdminInventoryQueryDTO queryDTO) {
        return adminInventoryService.getOutRecordsPage(queryDTO);
    }

    /**
     * 创建商品入库记录
     */
    @PostMapping("/records/in")
    @RequirePermission("admin:inventory:in:create")
    public Result<Void> createProductInRecord(@RequestParam Long productId,
                                             @RequestParam Integer quantity,
                                             @RequestParam(required = false) String remark) {
        return adminInventoryService.createProductInRecord(productId, quantity, remark);
    }

    /**
     * 创建商品分配出库记录
     */
    @PostMapping("/records/allocate")
    @RequirePermission("admin:inventory:allocate:create")
    public Result<Void> createAllocateOutRecord(@RequestParam Long productId,
                                               @RequestParam Long targetMerchantId,
                                               @RequestParam Integer quantity,
                                               @RequestParam(required = false) String remark) {
        return adminInventoryService.createAllocateOutRecord(productId, targetMerchantId, quantity, remark);
    }

    /**
     * 库存调整记录
     */
    @PostMapping("/records/adjust")
    @RequirePermission("admin:inventory:adjust:create")
    public Result<Void> createAdjustRecord(@RequestParam Long productId,
                                          @RequestParam Integer quantity,
                                          @RequestParam String recordType,
                                          @RequestParam(required = false) String remark) {
        return adminInventoryService.createAdjustRecord(productId, quantity, recordType, remark);
    }

    /**
     * 导出库存记录
     */
    @GetMapping("/records/export")
    @RequirePermission("admin:inventory:records:export")
    public Result<List<AdminInventoryRecordVO>> exportRecords(AdminInventoryQueryDTO queryDTO) {
        return adminInventoryService.exportRecords(queryDTO);
    }

    /**
     * 获取库存统计信息
     */
    @GetMapping("/statistics")
    @RequirePermission("admin:inventory:statistics:view")
    public Result<Object> getInventoryStatistics(@RequestParam(required = false) String startTime,
                                                 @RequestParam(required = false) String endTime) {
        return adminInventoryService.getInventoryStatistics(startTime, endTime);
    }
} 